/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.cn.xiaonuo.tcrfinance.tcrshops.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.StringUtils;
import com.cn.xiaonuo.core.exception.ServiceException;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.tcrfinance.tcrshops.entity.PvProductType;
import com.cn.xiaonuo.tcrfinance.tcrshops.enums.ProductTypeExceptionEnum;
import com.cn.xiaonuo.tcrfinance.tcrshops.mapper.PvProductTypeMapper;
import com.cn.xiaonuo.tcrfinance.tcrshops.param.PvProductTypeParam;
import com.cn.xiaonuo.tcrfinance.tcrshops.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class ProductServiceTypeImpl extends ServiceImpl<PvProductTypeMapper, PvProductType> implements ProductTypeService {

	@Autowired
	private PvProductTypeMapper pvProductTypeMapper;


	@Override
	public BusinessMessage queryProductTypeList(String name) {
		BusinessMessage<Object> message = new BusinessMessage<>();
		PvProductType pvProductType=new PvProductType();
		LambdaQueryWrapper<PvProductType> queryWrapper=new LambdaQueryWrapper<>();
		if(StringUtils.isNotEmpty(name)){
			queryWrapper.eq(PvProductType::getName,name);
		}
		queryWrapper.eq(PvProductType::getParentId,1000);
		queryWrapper.eq(PvProductType::getShowState,1);

		List<PvProductType> select = pvProductTypeMapper.selectList(queryWrapper);
		message.setSuccess(true);
		message.setCode("200");
		message.setData(select);
		return message;
	}

	@Override
	public PageResult<PvProductType>   page( PvProductTypeParam productTypeParam){
		QueryWrapper<PvProductType> queryWrapper = new QueryWrapper<>();
		if (ObjectUtil.isNotNull(productTypeParam)) {

			// 根据名称 模糊查询
			if (StringUtils.isNotBlank(productTypeParam.getName())) {
				queryWrapper.lambda().like(PvProductType::getName, productTypeParam.getName());
			}
			// 根据父级id 模糊查询
			if (ObjectUtil.isNotEmpty(productTypeParam.getParentId())) {
				queryWrapper.lambda().eq(PvProductType::getParentId, productTypeParam.getParentId());
			}
		}
		return new PageResult<PvProductType>(this.page(PageFactory.defaultPage(), queryWrapper));
	}

	@Override
	public void add(PvProductTypeParam productTypeParam) {
		PvProductType productType = new PvProductType();
		BeanUtil.copyProperties(productTypeParam, productType);
		this.save(productType);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void delete(PvProductTypeParam productTypeParam) {
		this.removeById(productTypeParam.getId());
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void edit(PvProductTypeParam productTypeParam) {

		PvProductType productType = this.queryProductType(productTypeParam);
		BeanUtil.copyProperties(productTypeParam, productType);
		productType.setCOrder(productTypeParam.getCOrder());
		this.updateById(productType);
	}

	@Override
	public PvProductType detail(PvProductTypeParam productTypeParam) {
		return this.queryProductType(productTypeParam);
	}

	/**
	 * 获取商品类别
	 *
	 * @author Monetary
	 * @date 2021-03-05 18:16:12
	 */
	private PvProductType queryProductType(PvProductTypeParam productTypeParam) {
		PvProductType productType = this.getById(productTypeParam.getId());
		if (ObjectUtil.isNull(productType)) {
			throw new ServiceException(ProductTypeExceptionEnum.NOT_EXIST);
		}
		return productType;
	}
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updateStatus(PvProductTypeParam productTypeParam) {
		PvProductType productType=this.detail(productTypeParam);
		productType.setShowState(productTypeParam.getShowState());
		this.updateById(productType);
	}

}
